home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / www / ludzie / txf / progs / savenames.lha / SaveNames1.5 / SaveNames.c < prev    next >
C/C++ Source or Header  |  1996-12-12  |  23KB  |  880 lines

  1.  
  2. /**************************************************************************
  3.  *                                                                        *
  4.  *                            Save Names 1.5                              *
  5.  *                           ----------------                             *
  6.  *                                                                        *
  7.  *  SaveNames is freely distributable but is copyrighted by its author,   *
  8.  * Marcin Orlowski (carlos@dedal.man.szczecin.pl). For more information   *
  9.  * read the short documentation included in this archive. SaveNames is    *
  10.  * written in ANSI C, so may be compiled on any machine, except for pc    *
  11.  * (do you see any sense? :). Unfortunately small changes must sometimes  *
  12.  * be done before you run the compiler. Look below, and select your       *
  13.  * platform. AMIGA side was tested on A4000, UNIX side was tested on IRIS *
  14.  * INDIGO, HP, SunOS (compiles harmless, however with small exceptions ). *
  15.  * The LINUX side was tested... on LINUX :-).                             * 
  16.  *                                                                        *
  17.  * Use and enjoy...                                                       *
  18.  *                                                                        *
  19.  * Any suggestions, bugs, mails or anything else send to:                 *
  20.  *                                                                        *
  21.  * Marcin Orlowski                     Tadek Knapik                       *
  22.  * Radomska 38                         Duza Gora 35/88                    *
  23.  * 71-002 Szczecin                     30-857 Krakow                      *
  24.  * Poland                              Poland                             *
  25.  *                                                                        *
  26.  * or via net:                                                            *
  27.  *                                                                        *
  28.  * carlos@dedal.man.szczecin.pl        tadek@student.uci.agh.edu.pl       *
  29.  * http://www.szczecin.pl/~carlos                                         *
  30.  *                                                                        *
  31.  * FidoNet:   Marcin Orlowski@2:481/22.2                                  *
  32.  * GlobalNet: Marcin Orlowski@52:4800/6                                   *
  33.  *                                                                        *
  34.  * or try Silver Dream!'s BBS (non-stop) at +48 91 540431                 *
  35.  *                                                                        *
  36.  *************************************************************************/
  37.  
  38. /* Define your favourite platform (except Amiga :-) */
  39.  
  40. #define AMIGA
  41.  
  42. /*                     NOTE: LINUX and SUNOS automagically defines
  43.                        UNIX by itself, so you don't need to
  44.                        uncomment it by hand!
  45. #define AMIGA
  46. #define LINUX
  47. #define SUNOS
  48. #define UNIX
  49. */
  50.  
  51. /* Uncomment this only for compilers requiring C&R declaration type */
  52. /*
  53. #define OLD_STYLE
  54. */
  55.  
  56. /*************************************************************************/
  57.  
  58. #ifdef AMIGA
  59. char __stdiowin[] = "con:0/15/500/230/W.F.M.H. SaveNames 1.5";
  60. char verstring[] = "$VER: SaveNames 1.5 (11.12.96)";
  61. #define CURRENTDIR ""
  62. #include <sys/dir.h>
  63.  
  64. #ifndef EXIT_SUCCESS
  65. #define EXIT_SUCCESS 0
  66. #endif
  67. #ifndef EXIT_WARN
  68. #define EXIT_WARN 5
  69. #endif
  70. #ifndef EXIT_FAILURE
  71. #define EXIT_FAILURE 20
  72. #endif
  73.  
  74. /* Do not touch this if you don't know what you are doing! */
  75. /*
  76. #ifdef MAXNAMELEN
  77. #undef MAXNAMELEN
  78. #endif
  79. #define MAXNAMELEN 108
  80. */
  81. #endif
  82.  
  83. /*************************************************************************/
  84.  
  85. #ifdef LINUX
  86. #include <unistd.h>
  87. #ifndef UNIX
  88. #define UNIX
  89. #endif
  90. #endif
  91.  
  92. /*************************************************************************/
  93.  
  94. #ifdef SUNOS
  95. #ifndef UNIX
  96. #define UNIX
  97. #endif
  98. #ifndef EXIT_SUCCESS
  99. #define EXIT_SUCCESS 0
  100. #endif
  101. #ifndef EXIT_FAILURE
  102. #define EXIT_FAILURE 20
  103. #endif
  104. #endif
  105.  
  106. /*************************************************************************/
  107.  
  108. #ifdef UNIX
  109. #ifdef CURRENTDIR
  110. #undef CURRENTDIR
  111. #endif
  112. #define CURRENTDIR "."
  113. #define PARENTDIR ".."
  114. #include <dirent.h>
  115. #include <sys/types.h>
  116. #endif
  117.  
  118. /*************************************************************************/
  119.  
  120. #include <sys/stat.h>
  121. #include <fcntl.h>
  122.  
  123. #include <stdio.h>
  124. #include <stdlib.h>
  125. #include <string.h>
  126. #include <ctype.h>
  127.  
  128. #ifndef RC_OK
  129. #define RC_OK         0        /* Tadek was here */
  130. #endif
  131. #define RC_SPLITERROR 1
  132. #define RC_CRAZY      2
  133. #define RC_CRAZY_COOL 3
  134. #ifndef RC_FAIL
  135. #define RC_FAIL       4
  136. #endif
  137.  
  138. #ifndef TRUE
  139. #define TRUE 1
  140. #endif
  141. #ifndef FALSE
  142. #define FALSE ~TRUE
  143. #endif
  144.  
  145. /*************************************************************************/
  146.  
  147. #define AMI_DD_OFS  853000        /* Capacity of various floppy disks   */
  148. #define AMI_DD_FFS  880000        /* This values may not fit exactly,   */
  149. #define AMI_HD_FFS 1780000        /* but it doesn't matter so much...   */
  150. #define PC_DD       725000        /* In case of big number of files,    */
  151. #define PC_HD      1455000        /* the disk space is going down, 'cos */
  152.  
  153. /* The capacity of pee-see floppy may differ, depending on the formatter
  154.    etc. If you format it on Amiga using multi_os.device you may want to
  155.    use the following values (don't forger to delete definitions above!):
  156.  
  157. #define PC_HD      1440000
  158. #define PC_DD       710000
  159. */
  160.                                   /* of file headers and similar things */
  161. /* Tadek: 3 next #defines */
  162.  
  163. #define PC_HD_FIT 1440000      /* this is for max file sizes -  a little bit */
  164. #define PC_DD_FIT 710000       /* lower, so it fits on disk for sure */
  165.  
  166. #define DISKS_MAX 999          /* you can't have more.. */
  167.  
  168. #define PCNAME  12             /* 8 + . + 3 */
  169.  
  170. FILE    *FileHandle = NULL;
  171. DIR     *dfd;                   /* directory descriptor */
  172. int     fh;                     /* FileHandle */
  173. struct  dirent  *dptr;          /* dir entry */
  174. struct  stat    *st = NULL;     /* file information */
  175.  
  176. char  TempBuff[PCNAME+1];       /* +1 because of trailing NULL */
  177. char  CurrentDir[MAXNAMLEN+2];
  178.  
  179. struct  Entry *FirstEntry= NULL;  /* for queue of entrires... */
  180. struct  Entry *LastEntry = NULL;
  181. struct  Entry *TempEntry = NULL;
  182. struct  Entry *Temp2     = NULL;
  183.  
  184. struct  Entry                     /* ... like this one...  */
  185. {
  186.     struct  Entry *Next;            /* Successor             */
  187.     char    INode[PCNAME+1];        /* Almost unique file ID */
  188.     char    Name[MAXNAMLEN+1];      /* Original file name    */
  189. };
  190.  
  191. int Index;                        /* Universal counter    */
  192. int Count = 0, SkipCount = 0;     /* Almost universal counters :) */
  193. unsigned long TotalSize = 0;      /* Guess yourself... */
  194. char  ErrorHeader = TRUE;
  195. char  DecodeResult;
  196.  
  197. /* Tadek - declarations */
  198.  
  199. int splitname(char s[], char t[], int splitsize);
  200. int rejoinname(char s[], char t[]);
  201. int namecount(char s[], char t[], int diskcount);
  202.  
  203. FILE *SpareIdx = NULL;
  204. char newname[MAXNAMLEN];
  205. int rc = NULL;
  206. int splitsize = NULL;
  207. int SplitCount = NULL;
  208.  
  209. /*************************************************************************/
  210.  
  211. #ifdef OLD_STYLE
  212. void main(args, argv)
  213. int args;
  214. char **argv;
  215. #else
  216. void main(int args, char **argv)
  217. #endif
  218. {
  219.     printf("\n");
  220.     printf("SaveNames 1.5 by Marcin Orlowski & Tadek Knapik\n");
  221.     printf("E-Mail: carlos@dedal.man.szczecin.pl\n");
  222.     printf("    or:  tadek@student.uci.agh.edu.pl\n\n");
  223.  
  224.     if((args < 3) || (args > 4))
  225.         {
  226. #ifdef AMIGA
  227.         if(args == 0)
  228.             printf("** Can't be run from Workbench.\n\n");
  229. #endif
  230.  
  231. #ifdef  UNIX
  232.         printf("Type 'man SaveNames'");
  233. #else
  234.         printf("Read file 'SaveNames.doc'");
  235. #endif
  236.  
  237.         printf(" to get whole documentation\n\n");
  238.  
  239.         if(strlen(argv[0]))
  240.             printf("Usage: %s ", argv[0]);
  241.         else
  242.             printf("Shell Usage: SaveNames ");
  243.  
  244.         printf("Command DirName <SplitSize>\n");
  245.         printf("Command   - type 'C'  or 'CODE' to crypt names (and optionally split files) \n");
  246.         printf("            type '-C' or '-COUNT' to count disks usage only\n");
  247.         printf("            type 'D'  or 'DECODE' to restore names and rejoin files\n");
  248.         printf("            type '-D' or '-DECODE' for expanded restore (also with rejoin)\n");
  249.         printf("DirName   - process contents of this directory\n");
  250.         printf("SplitSize - (optional) maximum size of output files (larger files will\n");
  251.         printf("            be splitted) or just 'DD'/'HD' (default PC sizes)\n\n");
  252.  
  253.         exit(EXIT_FAILURE);
  254.         }
  255.  
  256.  
  257.     getcwd(CurrentDir, sizeof(CurrentDir));
  258.     if(chdir(argv[2]))
  259.         {
  260.         printf("** Can't find directory.\n");
  261.         exit(EXIT_FAILURE);
  262.         }
  263.  
  264.  
  265.  
  266.     if(argv[1][0] == 'C' || argv[1][0] == 'c' || (argv[1][0] == '-' && argv[1][1] == 'C') || (argv[1][0] == '-' && argv[1][1] == 'c'))
  267.         {
  268.         if((dfd = opendir(CURRENTDIR)))
  269.             {
  270.             char running = TRUE;
  271.  
  272.             FileHandle = NULL;
  273.             if((FileHandle = fopen("WFMHFile.idx", "r")) == NULL)
  274.                 if((FileHandle = fopen("wfmhfile.idx", "r")) == NULL)
  275.                     FileHandle = fopen("WFMHFILE.IDX", "r");
  276.  
  277.             if(argv[1][0] == 'C' || argv[1][0] == 'c')
  278.                 {
  279.                 if(FileHandle)
  280.                     {
  281.                     fclose(FileHandle);
  282.                     printf("** Index file found in drawer '%s'.\n", argv[2]);
  283.                     printf("** It means you have already coded filenames.\n");
  284.                     printf("** Because this operation cannot be nested\n");
  285.                     printf("** it has been aborted. Watch your moves!\n");
  286.                     chdir(CurrentDir);
  287.                     exit(EXIT_FAILURE);
  288.                     }
  289.                 }
  290.  
  291.  
  292. /*
  293. ** Tadek: <SplitSize> handling
  294. */
  295.                 if (args == 4)
  296.                     {
  297.                     if (((strcmp(argv[3],"DD")) == 0) || ((strcmp(argv[3],"dd")) == 0) || ((strcmp(argv[3],"Dd")) == 0) || ((strcmp(argv[3],"dD")) == 0))
  298.                         splitsize = PC_DD;
  299.                     else if (((strcmp(argv[3],"HD")) == 0) || ((strcmp(argv[3],"hd")) == 0) || ((strcmp(argv[3],"Hd")) == 0) || ((strcmp(argv[3],"hD")) == 0))
  300.                         splitsize = PC_HD;
  301.                     else
  302.                         splitsize = atoi(argv[3]);
  303.  
  304.                         }
  305. /*
  306. ** end of splitsize handling
  307. */
  308.  
  309.             if(argv[1][0] == '-')
  310.                 printf("Counting floppy disks usage ...\n");
  311.             else
  312.                 {
  313.                 printf("Coding filenames");
  314.                 if (splitsize)          /* Tadek was here */
  315.                     printf(" (with splitsize %d bytes)", splitsize);
  316.                 printf("...\n");
  317.                 }
  318.  
  319.             Count = 0;
  320.             if((st = malloc(sizeof(struct stat))) == NULL)
  321.                 printf(" ** No length counting. No memory for structure.\n");
  322.  
  323.                 while((dptr = readdir(dfd)) && running)
  324.                     {
  325. #ifdef UNIX
  326.                     if((strcmp(dptr->d_name, CURRENTDIR) != 0) && (strcmp(dptr->d_name, PARENTDIR) != 0))
  327.                             {
  328. #endif
  329.                         if((TempEntry = (struct Entry *)malloc(sizeof(struct Entry))))
  330.                             {
  331.                             strcpy(TempEntry->Name, dptr->d_name);
  332.                             sprintf(TempEntry->INode, "%lx.pcp", dptr->d_ino);
  333.  
  334.                             if(!(FirstEntry))
  335.                                 FirstEntry = TempEntry;
  336.                             else
  337.                                 LastEntry->Next = TempEntry;
  338.                             LastEntry = TempEntry;
  339.                             TempEntry->Next = NULL;
  340.  
  341.                             if(st)
  342.                                 if(fh = open(dptr->d_name, O_RDONLY, 0))
  343.                                     {
  344.                                     if(fstat(fh, st) == 0)
  345.                                         TotalSize += st->st_size;
  346.                                     close(fh);
  347.                                     }
  348.  
  349.                             Count++;
  350.                             }
  351.                         else
  352.                             {
  353.                             printf("** Out of memory. %d files scanned.\n", Count);
  354.                             printf("** Skipping the rest of files.\n");
  355.                             running = FALSE;
  356.                             }
  357. #ifdef UNIX
  358.                         }
  359. #endif
  360.  
  361.                     }
  362.  
  363.             closedir(dfd);
  364.  
  365.             if(st)
  366.                 {
  367.                 if(TotalSize != 0)
  368.                     {
  369.                     int KB = (TotalSize/1024);
  370.                     int MB = (TotalSize/1048576);
  371.  
  372.                     printf("\n Disks summary for %ld files (%ld bytes", Count, TotalSize);
  373.                     if(KB)  printf(", %d KB", KB);
  374.                     if(MB)  printf(", %d MB", MB);
  375.  
  376.                     printf(")\n\n");
  377.                     printf(" Disk type      Capacity  Cnt  Free on last\n");
  378.                     printf(" ------------------------------------------\n");
  379.                     printf(" Amiga DD (OFS) %8ld %3ld   %9ld KB\n", AMI_DD_OFS, (AMI_DD_OFS + TotalSize) / AMI_DD_OFS, (AMI_DD_OFS * ((AMI_DD_OFS + TotalSize) / AMI_DD_OFS) - TotalSize)/1024);
  380.                     printf(" Amiga DD (FFS) %8ld %3ld   %9ld KB\n", AMI_DD_FFS, (AMI_DD_FFS + TotalSize) / AMI_DD_FFS, (AMI_DD_FFS * ((AMI_DD_FFS + TotalSize) / AMI_DD_FFS) - TotalSize)/1024);
  381.                     printf(" Amiga HD (FFS) %8ld %3ld   %9ld KB\n", AMI_HD_FFS, (AMI_HD_FFS + TotalSize) / AMI_HD_FFS, (AMI_HD_FFS * ((AMI_HD_FFS + TotalSize) / AMI_HD_FFS) - TotalSize)/1024);
  382.                     printf(" pc    DD       %8ld %3ld   %9ld KB\n",      PC_DD, (PC_DD + TotalSize) / PC_DD,           (PC_DD * ((PC_DD + TotalSize) / PC_DD) - TotalSize)/1024);
  383.                     printf(" pc    HD       %8ld %3ld   %9ld KB\n\n",    PC_HD, (PC_HD + TotalSize) / PC_HD,           (PC_HD * ((PC_HD + TotalSize) / PC_HD) - TotalSize)/1024);
  384.                     }
  385.                 else
  386.                     printf("No files - no summary :-(...\n");
  387.                 }
  388.     
  389.         if(st)
  390.                 {
  391.                 free(st);
  392.                 st = NULL;
  393.                 }
  394.  
  395.             if(argv[1][0] == '-')                 /* We wanted summary only */
  396.                 {
  397.                 TempEntry = FirstEntry;
  398.                 while(TempEntry)
  399.                     {
  400.                     Temp2 = TempEntry->Next;
  401.                     free(TempEntry);
  402.                     TempEntry = Temp2;
  403.                     }
  404.                 chdir(CurrentDir);
  405.                 exit(EXIT_SUCCESS);
  406.                 }
  407.  
  408.             if(FileHandle = fopen("WFMHFile.idx", "w"))
  409.                 {
  410.                 Count = 0;
  411.                 TempEntry = FirstEntry;
  412.  
  413. /*
  414. **  Tadek: my function is put instead of rename(), and I messed a little
  415. **         bit with the error handling
  416. */
  417.  
  418.                 while(TempEntry)
  419.                     {
  420.                     if((rc = splitname(TempEntry->Name, TempEntry->INode, splitsize)) != RC_OK)
  421.                         {
  422. #ifdef UNIX
  423.                     if((strcmp(TempEntry->Name, CURRENTDIR) != 0) && (strcmp(TempEntry->Name, PARENTDIR) != 0))
  424.                             {
  425. #endif
  426.                             if(ErrorHeader == TRUE)
  427.                                 {
  428.                                 printf("I had troubles with following files:\n");
  429.                                 printf("-------------------------------------\n");
  430.                                 ErrorHeader = FALSE;
  431.                                 }
  432.                             if (rc == RC_FAIL)
  433.                                 {
  434.                                 printf("Can't rename '%s'\n", TempEntry->Name);
  435.                                 SkipCount++;
  436.                                 }
  437.                             else
  438.                                 {
  439.                                 if ((rc == RC_SPLITERROR) || (rc == RC_CRAZY_COOL))
  440.                                     {
  441.                                     fprintf(FileHandle, "%s%c%s\n", TempEntry->INode, 0x09, TempEntry->Name);
  442.                                     Count++;
  443.                                     }
  444.                                 if (rc == RC_SPLITERROR)
  445.                                     {
  446.                                     printf("Can't split '%s'\n", TempEntry->Name);
  447.                                     }
  448.                                 else if ((rc == RC_CRAZY) || (rc == RC_CRAZY_COOL))
  449.                                     printf("Splitsize too small for '%s'\n", TempEntry->Name);
  450.                                 }
  451.                             }
  452. #ifdef UNIX
  453.                         }
  454. #endif
  455.                     else
  456.                         {
  457.                         fprintf(FileHandle, "%s%c%s\n", TempEntry->INode, 0x09, TempEntry->Name);
  458.                         Count++;
  459.                         }
  460.  
  461.                     Temp2 = TempEntry->Next;
  462.                     free(TempEntry);
  463.                     TempEntry = Temp2;
  464.                     }
  465.  
  466. /*
  467. **  Tadek: Processed files report - changed a little bit
  468. */
  469.  
  470.                 fclose(FileHandle);
  471.                 if(Count)
  472.                     if(SplitCount)
  473.                         printf("%d files processed (%d of them splitted)\n", Count, SplitCount);
  474.                     else
  475.                         {
  476.                         printf("%d files processed", Count);
  477.                         if (splitsize)
  478.                             printf(" (none splitted)");
  479.                         printf("\n");
  480.                         }
  481.                 if(SkipCount)
  482.                     printf("%d files skipped\n", SkipCount);
  483.                 if(Count)
  484.                     printf("Done.\n");
  485.                 }
  486.             else
  487.                 {
  488.                 TempEntry = FirstEntry;
  489.                 while(TempEntry);
  490.                     {
  491.                     Temp2 = TempEntry->Next;          
  492.                     free(TempEntry);
  493.                     TempEntry = Temp2;
  494.                     }
  495.                 printf("** Can't open index file to write 'WFMHFile.idx'.\n");
  496.                 chdir(CurrentDir);
  497.                 exit(EXIT_FAILURE);
  498.                 }
  499.             }
  500.         else
  501.             {
  502.             printf("** Can't open directory '%s'.\n", argv[2]);
  503.                     chdir(CurrentDir);
  504.             exit(EXIT_FAILURE);
  505.             }
  506.         }
  507.  
  508.  
  509. /*                             ########### DECODING PART BEGINS ##############   */
  510.  
  511.     else
  512.  
  513.         if(argv[1][0] == 'D' || argv[1][0] == 'd' || (argv[1][0] == '-' && argv[1][1] == 'D') || (argv[1][0] == '-' && argv[1][1] == 'd'))
  514.             {
  515.             if((TempEntry = malloc(sizeof(struct Entry))))
  516.                 {
  517.                 if((FileHandle = fopen("WFMHFile.idx", "r")) == NULL)
  518.                     if((FileHandle = fopen("wfmhfile.idx", "r")) == NULL)
  519.                         FileHandle = fopen("WFMHFILE.IDX", "r");
  520.  
  521.                 if(FileHandle)
  522.                     {
  523.                     if(argv[1][0] == '-')
  524.                     printf("Decoding filenames in expanded mode...\n");
  525.                         else
  526.                     printf("Decoding filenames...\n");
  527.  
  528.                     Count = 0;
  529.                     while(EOF != fscanf(FileHandle, "%s", TempEntry->INode))
  530.                         {
  531.  
  532.                         do
  533.                             TempEntry->Name[0] = getc(FileHandle);
  534.                         while((TempEntry->Name[0] != 0xa) && (TempEntry->Name[0] != 9));
  535.  
  536.                         for(Index=0; Index<MAXNAMLEN; Index++)
  537.                             {
  538.                             TempEntry->Name[Index] = fgetc(FileHandle);
  539.                             if(((TempEntry->Name[Index]) == 0xa) || (TempEntry->Name[Index] == EOF))
  540.                                     break;
  541.                             }
  542.                         TempEntry->Name[Index] = 0x00;
  543.  
  544. /*
  545. ** Tadek: again my function instead of rename()
  546. */
  547.  
  548.  
  549.                     if(rejoinname(TempEntry->INode, TempEntry->Name))
  550.                         {
  551.                         DecodeResult = FALSE;
  552.  
  553.                         if(argv[1][0] == '-')
  554.                             {
  555.                             Index = 0;
  556.                             while(TempEntry->INode[Index] != '\0')
  557.                                 {
  558.                                 TempEntry->INode[Index] = tolower((int)TempEntry->INode[Index]);
  559.                                 Index++;
  560.                                 }
  561.                             if(rejoinname(TempEntry->INode, TempEntry->Name))
  562.                                 {
  563.                                 Index = 0;
  564.                                 while(TempEntry->INode[Index] != '\0')
  565.                                     {
  566.                                     TempEntry->INode[Index] = toupper((int)TempEntry->INode[Index]);
  567.                                     Index++;
  568.                                     }
  569.                                 if(rejoinname(TempEntry->INode, TempEntry->Name) == 0)
  570.                                     {
  571.                                     Count++;
  572.                                     DecodeResult = TRUE;
  573.                                     }
  574.                                 }
  575.                             }
  576.  
  577.                         if(DecodeResult == FALSE)
  578.                             {
  579.                             if(ErrorHeader == TRUE)
  580.                                 {
  581.                                 printf("I can't restore following files:\n");
  582.                                 printf("--------------------------------\n");
  583.                                 SpareIdx = fopen("WFMHFil2.idx", "w");
  584.                                 ErrorHeader = FALSE;
  585.                                 }
  586.                             printf("'%s'\n", TempEntry->Name);
  587.                             SkipCount++;
  588.                             if (SpareIdx)
  589.                                 fprintf(SpareIdx, "%s%c%s\n", TempEntry->INode, 0x09, TempEntry->Name);
  590.                             }
  591.                         }
  592.                     else
  593.                         Count++;
  594.                         }
  595.  
  596.                     fclose(FileHandle);
  597.                     free(TempEntry);
  598.                     if(Count)
  599.                         {
  600.                         printf("%d files restored", Count);
  601.                         if (SplitCount)
  602.                             printf(" (%d of them rejoined)", SplitCount);
  603.                         printf("\n");
  604.                         }
  605.                     if(SkipCount)
  606.                         printf("%d files skipped\n", SkipCount);
  607.                     if(Count)
  608.                         printf("Done.\n");
  609.  
  610.                     if(SkipCount == 0)
  611.                         {
  612.                         if(remove("WFMHFile.idx") != 0)
  613.                             if(remove("wfmhfile.idx") != 0)
  614.                                 if(remove("WFMHFILE.IDX") != 0)
  615.                                     {
  616.                                     printf("** Can't remove index file.\n");
  617.                                     chdir(CurrentDir);
  618. #ifdef AMIGA
  619.                                     exit(EXIT_WARN);
  620. #endif
  621.                                     }
  622.                         }
  623.                     else
  624.                         {
  625.                         if (SpareIdx)
  626.                             {
  627.                             if(remove("WFMHFile.idx") != 0)
  628.                                 if(remove("wfmhfile.idx") != 0)
  629.                                     if(remove("WFMHFILE.IDX") != 0)
  630.                                         {
  631.                                         printf("** Can't remove old index file.\n");
  632.                                         chdir(CurrentDir);
  633. #ifdef AMIGA
  634.                                         exit(EXIT_WARN);
  635. #endif
  636.                                         }
  637.                             fclose(SpareIdx);
  638.                             SpareIdx = NULL;
  639.                             if ((rename("WFMHFil2.idx", "WFMHFile.idx")) != 0)
  640.                                 {
  641.                                 printf("** Can't rename new index.\n");
  642.                                 chdir(CurrentDir);
  643. #ifdef AMIGA
  644.                                 exit(EXIT_WARN);
  645. #endif
  646.                                 }
  647.                             }
  648.                         }
  649.                     }
  650.                 else
  651.                     {
  652.                     free(TempEntry);
  653.                     printf("** Can't find index file!\n");
  654.                     chdir(CurrentDir);
  655.                     exit(EXIT_FAILURE);
  656.                     }
  657.                 }
  658.             else
  659.                 {
  660.                 printf("** Can't allocate work buffer!\n");
  661.                 chdir(CurrentDir);
  662.                 exit(EXIT_FAILURE);
  663.                 }
  664.  
  665.             }
  666.         else
  667.             {
  668.             printf("** Unknown command.\n");
  669.             chdir(CurrentDir);
  670.             exit(EXIT_FAILURE);
  671.             }
  672.  
  673.     chdir(CurrentDir);
  674.     exit(EXIT_SUCCESS);
  675. }
  676.  
  677. /*************************************************************************/
  678.  
  679. /*
  680. ** Tadek - functions
  681. **
  682. ** return 0 for success
  683. */
  684.  
  685. #ifdef OLD_STYLE
  686. int splitname(readname, basename, splitsize)
  687. char readname[];
  688. char basename[];
  689. int splitsize;
  690. #else
  691. int splitname(char readname[], char basename[], int splitsize)
  692. #endif
  693. {
  694.  
  695.     FILE *RHandle = NULL;
  696.     FILE *WHandle = NULL;
  697.  
  698.     int c, i, j, disks, diskcount, rem;
  699.  
  700.     i = RC_FAIL;
  701.     if (splitsize != 0)
  702.         {
  703.         if((st = malloc(sizeof(struct stat))) == NULL)
  704.             {
  705.             if ((rename(readname, basename)) == 0)
  706.                 i = RC_SPLITERROR;
  707.             return i;
  708.             }
  709.         if(((stat(readname, st)) == 0) && (st->st_size > splitsize))
  710.             {
  711.             if((RHandle = fopen(readname, "r")) != 0)
  712.                 {
  713.                 disks = (st->st_size/splitsize);
  714.                 if (rem = ((st->st_size)%splitsize))
  715.                     ++disks;
  716.                 if(st)
  717.                     {
  718.                     free(st);
  719.                     st = NULL;
  720.                     }
  721.                 if (disks > DISKS_MAX)
  722.                     {
  723.                     fclose(RHandle);
  724.                     i = RC_CRAZY;
  725.                     if ((rename(readname, basename)) == 0)
  726.                         i = RC_CRAZY_COOL;
  727.                     return i;
  728.                     }
  729.                 for (diskcount = 0; diskcount < disks; ++diskcount)
  730.                     {
  731.                     if ((namecount(basename, newname, diskcount) == 0) || ((WHandle = fopen(newname, "w")) == 0))
  732.                         {
  733.                         fclose(RHandle);
  734.                         if ((rename(readname, basename)) == 0)
  735.                             i = RC_SPLITERROR;
  736.                         return i;
  737.                         }
  738.                     for (j = 0; (j < splitsize) && ((c=fgetc(RHandle)) != EOF); ++j)
  739.                         {
  740.                         if ((fputc(c, WHandle)) == EOF)
  741.                             {
  742.                             fclose(RHandle);
  743.                             fclose(WHandle);
  744.                             remove(newname);
  745.                             if ((rename(readname, basename)) == 0)
  746.                                 i = RC_SPLITERROR;
  747.                             return i;
  748.                             }
  749.                         }   
  750.                     fclose(WHandle);
  751.                     if (c == EOF)
  752.                         {
  753.                         fclose(RHandle);
  754.                         if (diskcount == disks-1)
  755.                             {
  756.                             remove(readname);
  757.                             i = RC_OK;
  758.                             SplitCount++;
  759.                             }
  760.                         else
  761.                             {
  762.                             remove(newname);
  763.                             if ((rename(readname, basename)) == 0)
  764.                                 i = RC_SPLITERROR;
  765.                             }
  766.                         return i;
  767.                         }
  768.                     }
  769.                 if (rem == 0)        /* the missing code - no more */
  770.                     {                /* chars to write, so no more disks */
  771.                     i = RC_OK;
  772.                     SplitCount++;
  773.                     }
  774.                 fclose(RHandle);
  775.                 remove(readname);
  776.                 }
  777.             if(st)
  778.                 {
  779.                 free(st);
  780.                 st = NULL;
  781.                 }
  782.             if ((rename(readname, basename)) == 0)
  783.                 i = RC_SPLITERROR;
  784.             return i;
  785.             }
  786.         if(st)
  787.             {
  788.             free(st);
  789.             st = NULL;
  790.             }
  791.         }
  792.     if ((rename(readname, basename)) == 0)
  793.         i = RC_OK;
  794.     return i;
  795. }
  796.  
  797.  
  798. /*
  799. **  now, let's join files (if needed)
  800. */
  801.  
  802. #ifdef OLD_STYLE
  803. int rejoinname(basename, realname)
  804. char basename[];
  805. char realname[];
  806. #else
  807. int rejoinname(char basename[], char realname[])
  808. #endif
  809. {
  810.  
  811.     int i, c, diskcount;
  812.     FILE *RHandle = NULL;
  813.     FILE *WHandle = NULL;
  814.  
  815.     i = RC_FAIL;
  816.  
  817.     if ((rename(basename, realname)) == 0)
  818.         {
  819.         i = RC_OK;
  820.         return i;
  821.         }
  822.     for (diskcount = 0; ((namecount(basename, newname, diskcount)) != 0) && ((RHandle = fopen(newname, "r")) !=0); ++diskcount)
  823.         {
  824.         if (WHandle == NULL)
  825.             if ((WHandle = fopen(realname, "w")) == 0)
  826.                 {
  827.                 fclose(RHandle);
  828.                 return i;
  829.                 }
  830.         while ((c=fgetc(RHandle)) != EOF)
  831.             fputc(c, WHandle);
  832.         fclose(RHandle);
  833.         remove(newname);
  834.         }
  835.     if (WHandle)
  836.         {
  837.         fclose(WHandle);
  838.         SplitCount++;
  839.         i = RC_OK;
  840.         }
  841.     return i;
  842.  
  843. }
  844.  
  845.  
  846. /*
  847. **  This is for constructing numbered file names
  848. */
  849.  
  850. #ifdef OLD_STYLE
  851. int namecount(base, newname, suffix)
  852. char base[];
  853. char newname[];
  854. int suffix;
  855. #else
  856. int namecount(char base[], char newname[], int suffix)
  857. #endif
  858. {
  859.  
  860.     int i;
  861.  
  862.     strcpy(newname, base);
  863.  
  864.     for (i = 0; newname[i] != 0; ++i)
  865.         ;
  866.     --i;
  867.     if (suffix > 9)
  868.         --i;
  869.     if (suffix > 99)
  870.         --i;
  871.     if (suffix > DISKS_MAX)
  872.         return 0;
  873.  
  874.     if ((sprintf(&newname[i],"%d", suffix)) > 3)
  875.         return 0;
  876.     return i;
  877.  
  878. }
  879.  
  880.